package com.zhuss.product.processor;


import java.util.*;
import java.util.stream.Collectors;

import com.zhuss.bean.bo.product.train.CreateLineStrategyBo;
import com.zhuss.bean.bo.product.train.DetailLineStrategyBo;
import com.zhuss.bean.unit.linesite.BoardBean;
import com.zhuss.bean.unit.linesite.MsgResp;
import com.zhuss.bean.vo.product.site.DetailSiteVo;
import com.zhuss.bean.vo.product.train.ListLineStrategySiteVo;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.OperationStatus;
import com.zhuss.product.db.dao.ext.LineStrategySiteDao;
import com.zhuss.product.db.dao.ext.PrelineSignDao;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.processor.cache.SiteCache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.train.SearchLineStrategyBo;
import com.zhuss.bean.vo.product.train.DetailLineStrategyVo;
import com.zhuss.bean.vo.product.train.ListLineStrategyVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.db.entity.LineStrategy;
import com.zhuss.util.DateUtil;


/**
 * @description: 线路策略读
 * @author: zhushanshuo
 * @create: Mon Jan 04 09:51:07 CST 2021
 **/
@Component("lineStrategyRead")
public class LineStrategyRead {

    @Autowired
    private LineStrategyDao lineStrategyDao;

    @Autowired
    private TrainDao trainDao;

    @Autowired
    private LineStrategySiteDao lineStrategySiteDao;

    @Autowired
    @Qualifier("siteCache")
    private SiteCache siteCache;

    @Autowired
    private PrelineSignDao prelineSignDao;


    public List<ListLineStrategyVo> listByType(Integer lineType) {
    	Date now = DateUtil.toDateOnly(new Date());
        List<ListLineStrategyVo> result = lineStrategyDao.listByType(lineType, OperationStatus.已开通.key(), now);
        return result;
    }


    @SuppressWarnings("unused")
    public Map<String, LineStrategy> listMap() {
        List<ListLineStrategyVo> list = lineStrategyDao.listAll();
        Map<String, LineStrategy> map = new HashMap<>();
//		for (LineStrategy c : list) {
//			map.put(c.getName(), c);
//		}
        return map;
    }

    /**
     * 线路策略
     *
     * @return
     */
    public List<ListLineStrategyVo> listLineStrategy() {
        List<ListLineStrategyVo> result = lineStrategyDao.listAll();
        return result;
    }

    /**
     * 线路策略
     *
     * @return
     */
    public Object listLineStrategySelect() {
        List<ListLineStrategyVo> lineStrategyList = listLineStrategy();
        List<ListVo> result = new ArrayList<>();
        transferList(lineStrategyList,result);
        return result;
    }


    /**
     * 线路策略
     *
     * @return
     */
    public List<ListLineStrategyVo> listLineStrategy(List<Long> lineStrategyIdList) {
    	Date now = DateUtil.toDateOnly(new Date());
        List<ListLineStrategyVo> result = lineStrategyDao.listById(lineStrategyIdList, OperationStatus.已开通.key(), now);
        return result;
    }


    /**
     * 根据名字查询线路策略
     *
     * @param
     * @return
     */
    public List<LineStrategy> list(String name) {
        List<LineStrategy> result = lineStrategyDao.listByName(name);
        return result;
    }

    public List<LineStrategy> listNotId(String name,Long lineStrategyId) {
        List<LineStrategy> result = new ArrayList<LineStrategy>();
        List<LineStrategy> list = list(name);
        for (LineStrategy c : list) {
            if (!c.getLineStrategyId().equals(lineStrategyId)) {
                result.add(c);
                break;
            }
        }
        return result;
    }

    /**
     * 线路策略
     *
     * @return
     */
    public Object listLineStrategyId() {
        List<ListLineStrategyVo> lineStrategyList = lineStrategyDao.listAll();
        List<Long> result = new ArrayList<>();
        transferListId(lineStrategyList,result);
        return result;
    }

    private void transferListId(List<ListLineStrategyVo> lineStrategyList,List<Long> result) {
        for (ListLineStrategyVo item : lineStrategyList) {
            result.add(item.getLineStrategyId());
        }
    }

    private void transferList(List<ListLineStrategyVo> lineStrategyList,List<ListVo> result) {
        for (ListLineStrategyVo item : lineStrategyList) {
            ListVo vo = new ListVo();
            vo.setKey(item.getLineStrategyId());
//			vo.setLabel(item.getName());
            result.add(vo);
        }

    }

    /**
     * 线路策略列表
     *
     * @param bo
     * @return
     */
    public Object list(SearchLineStrategyBo bo) {
        PageVo<ListLineStrategyVo> result = new PageVo<ListLineStrategyVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(lineStrategyDao.list(bo));
        result.setTotal(lineStrategyDao.countList(bo));
        return result;
    }

    /**
     * 线路策略详情
     *
     * @param lineStrategyId
     * @return
     * @throws DzcxException
     */
    public DetailLineStrategyVo detail(Long lineStrategyId) throws DzcxException {
        DetailLineStrategyVo result = lineStrategyDao.detail(lineStrategyId);
        if (result == null) {
            throw new DzcxException(ExceptionCode.数据不存在);
        }
        return result;
    }

    public Object checkTrain(CreateLineStrategyBo bo) throws DzcxException {
        List<BoardBean> strategyResp = new ArrayList<BoardBean>();
        //根据模板列表、班次类别查询t_line_strategy
        List<ListLineStrategyVo> strategyVoList = lineStrategyDao.selectByBoard(bo.getBoardList(),
                bo.getStrategyType(),bo.getLineId(), bo.getStartDate(), bo.getEndDate());
        List<Long> trainList = new ArrayList<Long>();
        Set<Long> longSet = new HashSet<Long>();
        if (strategyVoList.size() > 0) {
        	Date now = DateUtil.toDateOnly(new Date());
            longSet = trainDao.getTrainByDate(bo.getStartDate(),bo.getEndDate(),strategyVoList,
                    Arrays.asList(OperationStatus.未开通.key(),OperationStatus.已开通.key()), now);
            trainList.addAll(longSet);
        }

        if (trainList == null || trainList.size() == 0) {
            return strategyResp;
        }
        List<ListLineStrategyVo> listLineStrategyVos = strategyVoList.stream().
                filter(vo -> trainList.contains(vo.getLineStrategyId())).
                collect(Collectors.toList());

        if (listLineStrategyVos == null || listLineStrategyVos.size() ==0) {
            return strategyResp;
        }
        //组装数据
        for (BoardBean bean : bo.getBoardList()) {
            List<MsgResp> respList = new ArrayList<MsgResp>();
            for (ListLineStrategyVo vo : listLineStrategyVos) {
                if (vo.getBoardNo().equals(bean.getNo())) {
                    MsgResp msgResp = new MsgResp();
                    msgResp.setStartDate(vo.getStartDate());
                    msgResp.setEndDate(vo.getEndDate());
                    respList.add(msgResp);
                }
                bean.setMsgRespList(respList);
            }
            strategyResp.add(bean);
        }
        return strategyResp;
    }

    public Object getSiteList(DetailLineStrategyBo bo) throws DzcxException {
        List<ListLineStrategySiteVo> siteVoList = lineStrategySiteDao.listByStrategy(bo.getLineStrategyId());
        for (ListLineStrategySiteVo site : siteVoList) {
            DetailSiteVo detailSiteVo = siteCache.get(site.getSiteId());
            site.setSiteName(detailSiteVo.getName());
            site.setLng(detailSiteVo.getLng());
            site.setLat(detailSiteVo.getLat());
        }
        return siteVoList;
    }

    /**
     * 新线招募列表
     * @param bo
     * @return
     * @throws DzcxException
     */
    public Object newLineList (SearchLineStrategyBo bo) throws DzcxException {
        bo.setCanSell(IsEnum.否.key());
        PageVo<ListLineStrategyVo> result = new PageVo<ListLineStrategyVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        List<ListLineStrategyVo> strategyVoList = lineStrategyDao.list(bo);
        for (ListLineStrategyVo vo : strategyVoList) {
            String[] siteStr = vo.getLineTypeName().split("-");
            vo.setStartPointName(siteStr[0]);
            vo.setEndPointName(siteStr[1]);
            vo.setApplyCount(prelineSignDao.countByPrelineId(vo.getLineStrategyId()));

        }
        result.setItems(lineStrategyDao.list(bo));
        result.setTotal(lineStrategyDao.countList(bo));
        return result;
    }


}
